package com.sudo.controller;

import com.sudo.aspect.AuthorizationApi;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.pojo.SysDict;
import com.sudo.pojo.bo.SysDictBO;
import com.sudo.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author：为道日损 2020-06-23 17:04
 * webSite：https://www.sudo.ren
 */
@Api(value = "数据字典",tags = {"数据字典相关接口"})
@RestController
@RequestMapping("sysDict")
public class SysDictController extends BaseController{

    @Autowired
    private SysDictService sysDictService;

    @AuthorizationApi(menu = "dictRun",button = "add")
    @ApiOperation(value = "新增父级数据字典",notes = "新增父级数据字典",httpMethod = "POST")
    @PostMapping("addParentDict")
    public JsonRes addParentDict(@RequestBody @Valid SysDictBO sysDictBO,
                                    BindingResult result,
                                    HttpServletRequest request) {
        //验证
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        //1.只允许超级管理员操作
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级用户操作");
        }
        //2.判断当前dictCode是否被占用
        SysDict sysDict = sysDictService.getSysDictByCode(sysDictBO.getDictCode());
        if (null != sysDict) {
            return JsonRes.errorMsg("当前字典代码已占用");
        }
        int affectCount = sysDictService.addDict(sysDictBO, sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "dictRun",button = "add")
    @ApiOperation(value = "新增子级数据字典",notes = "新增子级数据字典",httpMethod = "POST")
    @PostMapping("addChildDict")
    public JsonRes addChildDict(@RequestBody @Valid SysDictBO sysDictBO,
                                 BindingResult result,
                                 HttpServletRequest request) {
        //验证
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        //1.只允许超级管理员操作
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级用户操作");
        }
        //2.判断父级是否存在
        SysDict sysParentDict = sysDictService.getSysDictByCode(sysDictBO.getpCode());
        if (null == sysParentDict) {
            return JsonRes.errorMsg("当前父级字典不存在");
        }
        //3.判断当前dictCode是否被占用
        SysDict sysDict = sysDictService.getSysDictByCode(sysDictBO.getDictCode());
        if (null != sysDict) {
            return JsonRes.errorMsg("当前字典代码已占用");
        }

        int affectCount = sysDictService.addDict(sysDictBO, sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "dictRun",button = "search")
    @ApiOperation(value = "分页查询父级字典",notes = "分页查询父级字典",httpMethod = "GET")
    @GetMapping("getParentDictByPage")
    public JsonRes getParentDictByPage(@RequestParam Integer page,
                                       @RequestParam Integer pageSize) {

        PagedGridResult pagedGridResult = sysDictService.getParentDictByPage(page, pageSize);
        return JsonRes.ok(pagedGridResult);
    }

    @AuthorizationApi(menu = "dictRun",button = "search")
    @ApiOperation(value = "根据pCode查询字典",notes = "根据pCode查询字典",httpMethod = "GET")
    @GetMapping("getChildDictByPCode")
    public JsonRes getChildDictByPCode(@RequestParam("pCode") String pCode) {

        List<SysDict> sysDictList = sysDictService.getChildDictByPCode(pCode);
        return JsonRes.ok(sysDictList);
    }

    @AuthorizationApi(menu = "dictRun",button = "update")
    @ApiOperation(value = "根据dictCode修改字典",notes = "根据dictCode修改字典",httpMethod = "POST")
    @PostMapping("updateDictByDictCode")
    public JsonRes updateDictByDictCode(@RequestBody @Valid SysDictBO sysDictBO,
                                    BindingResult result,
                                    HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        //1.只允许超级管理员操作
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级用户操作");
        }
        int affectCount = sysDictService.updateDictByDictCode(sysDictBO,sessionUser);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "dictRun",button = "search")
    @ApiOperation(value = "根据dictCode查询字典",notes = "根据dictCode查询字典",httpMethod = "GET")
    @GetMapping("getSysDictByDictCode")
    public JsonRes getSysDictByDictCode(@RequestParam("dictCode") String dictCode) {
        SysDict sysDict = sysDictService.getSysDictByCode(dictCode);
        return JsonRes.ok(sysDict);
    }

    @AuthorizationApi(menu = "dictRun",button = "delete")
    @ApiOperation(value = "根据dictCode删除字典",notes = "根据dictCode删除字典",httpMethod = "POST")
    @PostMapping("deleteDictByCode")
    public JsonRes deleteDictByCode(@RequestParam("dictCode") String dictCode) {
        int affectCount = sysDictService.deleteDictByCode(dictCode);
        return JsonRes.ok(affectCount);
    }

}
